﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Groupware
{
    public class Event
    {
        #region Fields

        private Storage.EntityModels.Event dbEvent;
        private Notification notify;
        
        #endregion

        #region Constructors

        public Event(int creatorID)
        {
            dbEvent = new Storage.EntityModels.Event();
            notify = new Notification();
            Creator = creatorID;
        }

        public Event(Storage.EntityModels.Event e)
        {
            dbEvent = e;
            notify = new Notification();
        }
        
        #endregion

        #region Notification

        public void EventNotification(bool delete)
        {
            if (delete)
            {
                notify.TemplateType = Notification.EVENT_DELETED;
            }
            else
            {
                notify.TemplateType = Notification.EVENT_MODIFIED;
            }
            notify.SenderID = this.Creator;
            notify.EventId = this.dbEvent.Id;
            List<int> lst = getUsersToBeNotified();
            notify.Store(lst);
        }

        private void notifySomething(String thing, Object oldValue, Object newValue)
        {
            if (notifiable())
            {
                notify.AddField("New" + thing, newValue);
            }
        }

        private bool notifiable()
        {
            //return this.Id != null;
            return true;
        }

        private List<int> getUsersToBeNotified()
        {
            return new List<int>();
        }

        #endregion

        #region Associations

        public void JoinEvent(int userId)
        {
            Storage.EntityModels.User u = Storage.StorageManager.Instance.GetUserById(userId);
            Storage.EntityModels.Users_Event users_events = new Storage.EntityModels.Users_Event();
            users_events.Event = dbEvent;
            users_events.EventId = Id;
            users_events.User = u;
            users_events.UserId = userId;
            dbEvent.Users_Events.Add(users_events);
            Update();
        }
        
        public void AssociateTrip(int tripId)
        {
            Storage.EntityModels.Trip trip = Storage.StorageManager.Instance.GetTripById(tripId);
            Storage.EntityModels.Trips_Event tr_ev = new Storage.EntityModels.Trips_Event();
            tr_ev.Trip = trip;
            tr_ev.TripId = tripId;
            tr_ev.Event = dbEvent;
            tr_ev.EventId = Id;
            dbEvent.Trips_Events.Add(tr_ev);
            Update();
        }

        #endregion

        #region Properties

        public Storage.EntityModels.Event DbEvent
        {
            get { return dbEvent; }
            internal set { dbEvent = value; }
        }

        public int Id
        {
            get { return dbEvent.Id; }

        }

        public int Creator
        {
            get { return dbEvent.Creator; }
            private set { dbEvent.Creator = value; }

        }

        public String Type
        {
            get { return dbEvent.Type; }
            set { dbEvent.Type = value; }
        }

        public String Photo
        {
            get { return dbEvent.Photo; }
            set { dbEvent.Photo = value; }
        }

        public String Link
        {
            get { return dbEvent.Link; }
            set { dbEvent.Link = value; }
        }

        public Nullable<decimal> Price
        {
            get { return dbEvent.Price; }
            set {
                    notifySomething("Price", Price, value);
                    dbEvent.Price = value;
                }
        }

        public Storage.EntityModels.Place Place
        {
            get { return dbEvent.Place; }
            set
            {
                notifySomething("Place", Place, value);
                dbEvent.Place = value;
            }
        }

        public DateTime Date
        {
            get { return dbEvent.Date; }
            set
            {
                notifySomething("Date", Date, value);
                dbEvent.Date = value;
            }
        }

        #endregion

        #region StoreMethods

        private void Delete()
        {
            Storage.EntityModels.TripLoaDataContext context = Storage.StorageManager.Instance.Entities;
            context.Events.DeleteOnSubmit(this.dbEvent);
            context.Users_Events.DeleteAllOnSubmit(this.dbEvent.Users_Events);
            context.Trips_Events.DeleteAllOnSubmit(this.dbEvent.Trips_Events);
            context.SubmitChanges();
        }

        private void Update()
        {
            Storage.StorageManager.Instance.Entities.SubmitChanges();
        }

        private void Store()
        {
            Storage.EntityModels.TripLoaDataContext context = Storage.StorageManager.Instance.Entities;
            context.Events.InsertOnSubmit(this.dbEvent);
            context.Users_Events.InsertAllOnSubmit(this.dbEvent.Users_Events);
            context.Trips_Events.InsertAllOnSubmit(this.dbEvent.Trips_Events);
            context.SubmitChanges();
        }

        #endregion
    }
}
